using System.Drawing.Text;

namespace 订单窗体
{
    public partial class Form1 : Form
    {
        private Order CurrentOrder;
        private List<Customer> Customers = new List<Customer>();
        private OrderService orderService = new OrderService();
        private BindingSource orderBindingSource = new BindingSource();
        private BindingSource detailBindingSource = new BindingSource();
        private void SetupDataBindings()
        {
            //CurrentOrder = (Order)orderBindingSource.Current;
            //rderBindingSource.DataSource = orderService.GetAllOrders();
            //detailBindingSource.DataSource =orderBindingSource;
            //detailBindingSource.DataMember = "DetailList"; 
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = orderBindingSource;

            //dataGridView2.AutoGenerateColumns = false;
           // dataGridView2.DataSource = detailBindingSource;

            // 设置列
            SetupOrderGridColumns();
            SetupDetailGridColumns();
        }
        private void SetupOrderGridColumns()
        {
            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "OrderId",
                HeaderText = "订单号",
                Name = "colOrderId",
                Width = 150
            });

            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "Customer",
                HeaderText = "客户",
                Name = "colCustomer",
                Width = 200,
                ValueType = typeof(Customer)
            });

            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "TotalAmount",
                HeaderText = "总金额",
                Name = "colTotalAmount",
                Width = 100,
                DefaultCellStyle = new DataGridViewCellStyle() { Format = "C2" }
            });
        }

        private void SetupDetailGridColumns()
        {
            dataGridView2.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "Product",
                HeaderText = "商品",
                Name = "colProduct",
                Width = 200,
                ValueType = typeof(Product)
            });

            dataGridView2.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "Quantity",
                HeaderText = "数量",
                Name = "colQuantity",
                Width = 80
            });

            dataGridView2.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "Amount",
                HeaderText = "金额",
                Name = "colAmount",
                Width = 100,
                DefaultCellStyle = new DataGridViewCellStyle() { Format = "C2" }
            });
        }
        private void LoadSampleData()
        {
            // 加载一些示例数据
            var customer1 = new Customer("C001", "张三");
            var customer2 = new Customer("C002", "李四");

            var product1 = new Product("P001", "笔记本电脑", 5999.99);
            var product2 = new Product("P002", "手机", 3999.99);
            var product3 = new Product("P003", "耳机", 299.99);

            var order1 = new Order(customer1, "O001");
            order1.AddDetail(new Orderdetail(product1, 1));
            order1.AddDetail(new Orderdetail(product3, 2));

            var order2 = new Order(customer2, "O002");
            order2.AddDetail(new Orderdetail(product2, 1));
            order2.AddDetail(new Orderdetail(product3, 1));

            Customers.Add(customer1);
            Customers.Add(customer2);
            orderService.Add(order1);
            orderService.Add(order2);
        }
        private void RefreshOrders()
        {
            orderBindingSource.DataSource = null;
            orderBindingSource.DataSource = orderService.GetAllOrders();
        }
        public Form1()
        {

            InitializeComponent();
            LoadSampleData();
            RefreshOrders();
            SetupDataBindings();
            //RefreshOrders();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            var queryForm = new Form3(orderService);
            if (queryForm.ShowDialog() == DialogResult.OK)
            {
                orderBindingSource.DataSource = null;
                orderBindingSource.DataSource = queryForm.QueryResult;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var editForm = new Form2(null, orderService,Customers);
            if (editForm.ShowDialog() == DialogResult.OK)
            {
                RefreshOrders();
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //CurrentOrder = (Order)orderBindingSource.Current;
            //detailBindingSource.DataSource = null;
           // detailBindingSource.DataSource = CurrentOrder.DetailList;
        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (orderBindingSource.Current == null)
            {
                MessageBox.Show("请先选择一个订单", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            var selectedOrder = (Order)orderBindingSource.Current;
            var editForm = new Form2(selectedOrder, orderService,Customers);
            if (editForm.ShowDialog() == DialogResult.OK)
            {
                RefreshOrders();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (orderBindingSource.Current == null)
            {
                MessageBox.Show("请先选择一个订单", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            var selectedOrder = (Order)orderBindingSource.Current;
            if (MessageBox.Show($"确定要删除订单 {selectedOrder.OrderId} 吗?", "确认删除",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    orderService.Remove(selectedOrder.OrderId);
                    RefreshOrders();
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"删除失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}
